package app.yj.channel.strategy;

import android.support.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;


/**
 * Anthor:Cpb
 * Time:2019/6/28
 * Desc:博客请求策略
 */
public class BlogStrategy {

    public static final String TYPE_HTTP_DBLOG = "dBlog"; // 国内博客
    public static final String TYPE_HTTP_FBLOG = "fBlog"; // 国外博客

    private int dBlogSize = 0;
    private int fBlogSize = 0;
    private IRequestCallBack requestCallBack;

    public BlogStrategy(int dBlogSize, int fBlogSize, @NonNull IRequestCallBack requestCallBack) {
        this.dBlogSize = dBlogSize;
        this.fBlogSize = fBlogSize;
        this.requestCallBack = requestCallBack;
    }

    /**
     * 状态池
     */
    private List<HttpResult> dBlog = new ArrayList<>();
    private List<HttpResult> fBlog = new ArrayList<>();


    public void setHttpBlog(String type, HttpResult channelResult) {
        if (TYPE_HTTP_DBLOG.equals(type))
            dBlog.add(channelResult);
        else if (TYPE_HTTP_FBLOG.equals(type))
            fBlog.add(channelResult);

        doStrategy();
    }


    /**
     * 先 国内Blog ---> 国外Blog
     */
    private synchronized void doStrategy() {
        //1.国内blog;存在就处理,不存在下一次处理.
        if (dBlogSize > 0) {
            if (dBlog.size() <= 0) {
                return;
            } else {
                if (isAllCancel(dBlog)) {
                    return;
                }
                //1.1  返回成功处理,失败不做任何处理进入下一轮.
                for (HttpResult httpResult : dBlog) {
                    if (httpResult.status) {
                        httpResult.callback.doCallback();
                        cancelAllHttp();
                        return;
                    }
                }

                if (dBlog.size() != dBlogSize) {
                    return;
                }
            }
        }

        //国外Blog
        if (fBlogSize > 0) {
            if (fBlog.size() <= 0) {
                return;
            } else {
                if (isAllCancel(fBlog)) {
                    return;
                }
                //1.1  返回成功处理,失败不做任何处理进入下一轮.
                for (HttpResult httpResult : fBlog) {
                    if (httpResult.status) {
                        httpResult.callback.doCallback();
                        cancelAllHttp();
                        return;
                    }


                }

                if (fBlog.size() != fBlogSize) {
                    return;
                }
            }
        }
        if (requestCallBack != null) {
            requestCallBack.requestFail();
        }

    }

    /**
     * 关闭所有 的请求状态.
     */
    private void cancelAllHttp() {

        if (dBlog != null) {
            for (HttpResult httpResult : dBlog) {
                httpResult.hasCanceled = true;
            }
        }
        if (fBlog != null) {
            for (HttpResult httpResult : fBlog) {
                httpResult.hasCanceled = true;
            }
        }
    }


    /**
     * 判断
     *
     * @param pool
     * @return
     */
    private boolean isAllCancel(List<HttpResult> pool) {
        for (HttpResult httpResult : pool) {
            if (httpResult.hasCanceled)
                return true;
        }
        return false;
    }

    /**
     * 结果状态类,策略用来处结果.  不能完全按,权重处理.
     */
    public static class HttpResult {
        public HttpResult(String type, boolean status, @NonNull IResultCallBack callback) {
            this.type = type;
            this.status = status;
            this.callback = callback;
        }

        public String type;    //权重类型, leancloud\bmob\blog
        public boolean status;   //请求成功,失败
        public boolean hasCanceled;   //手动关闭停止.
        public IResultCallBack callback;   //回调
    }

    public interface IResultCallBack {
        void doCallback();
    }

    public interface IRequestCallBack {
        void requestFail();
    }

}